package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hqyj.pojo.Paper;
import com.hqyj.dao.PaperMapper;
import com.hqyj.pojo.PaperQuestionRel;
import com.hqyj.pojo.ResultVo;
import com.hqyj.service.IPaperQuestionRelService;
import com.hqyj.service.IPaperService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2022-09-28
 */
@Service
public class PaperServiceImpl extends ServiceImpl<PaperMapper, Paper> implements IPaperService {

    //注入mapper
    @Resource
    PaperMapper paperMapper;

    // 注入PaperQuestionService接口实现类
    @Autowired
    IPaperQuestionRelService iPaperQuestionRelService;

    //注入redis
    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    @Transactional//添加事务
    @Override
    public ResultVo paperAdd(Paper p) {
        ResultVo rv = new ResultVo();
        //试卷重名判断
        QueryWrapper<Paper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("paper_name",p.getPaperName());
        Paper paper = paperMapper.selectOne(queryWrapper);
        if(paper!=null){
            rv.setInfo("试卷重名");
            return rv;
        }
        //插入试卷表
        int num = paperMapper.insert(p);
        if(num>0){
            //获取选中的试题id
            String quIds[] = p.getQuIds().split(",");
            List<PaperQuestionRel> list = new LinkedList<>();
            for(String s:quIds){
                PaperQuestionRel pqr = new PaperQuestionRel();
                pqr.setPaperId(p.getPaperId());
                pqr.setQuestionId(Integer.parseInt(s));
                list.add(pqr);
            }
            // 批量插入
            boolean is = iPaperQuestionRelService.saveBatch(list);
            if(is){
                rv.setInfo("试卷生成成功");
                rv.setSuccess(true);
                return rv;
            }

        }
        rv.setInfo("试卷生成失败");

        return rv;
    }

    @Override
    public ResultVo showPaper(String name) {
        ResultVo rv = new ResultVo();
        Paper p = paperMapper.queryPaperByName(name);
        if(p==null){
            rv.setInfo("没有试卷");
            return rv;
        }
        rv.setSuccess(true);
        rv.setData(p);
        return rv;
    }

    @Override
    public ResultVo queryPaperByName(String name) {
        //创建一个缓存建名
        String key = "exam:"+name;
        ResultVo rv = new ResultVo();

        Object obj = redisTemplate.opsForValue().get(key);
        Paper p =null;
        //判断缓存是否有数据
        if(obj==null){
             p = paperMapper.selectPaperByName(name);
            //
            redisTemplate.opsForValue().set(key,p,30, TimeUnit.MINUTES);
        }else{
            //查询缓存
            p=(Paper)obj;
        }
        if(p==null){
            rv.setInfo("没有试卷");
            return rv;
        }
        rv.setSuccess(true);
        rv.setData(p);
        return rv;
    }
}
